/**
* 基础菜单 部门管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>部门管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline"  ref="formInline" class="user-search" >
      <el-form-item label="搜索：" prop="deptName">
        <el-input size="small" v-model="formInline.deptName" placeholder="请输入部门名称" @keyup.enter.native="search"></el-input>
      </el-form-item>


      <el-form-item  label=""  prop="status" >
        <el-select size="small" v-model="formInline.status" placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="启用" value="0"></el-option>
          <el-option label="禁用" value="1"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button> <el-button size="small" type="primary" icon="el-icon-delete" @click="clearForm">清空</el-button>
      </el-form-item>

    </el-form>

    <el-row size="small">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addDept" v-has="'sys:dept:add'">添加</el-button>
    </el-row>
    <br>

    <!-- 部门列表 -->
    <el-table
      v-loading="loading"
      :data="deptData"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">

      <el-table-column align="left"  prop="label" label="部门名称" width="240">
      </el-table-column>

      <el-table-column align="center"  prop="orderNum" label="部门排序" width="240">
      </el-table-column>

      <el-table-column align="center"  prop="status" label="部门状态" width="240">
        <template slot-scope="scope">
         <!-- <div v-if="scope.row.status==1">禁用</div>
          <div v-else="scope.row.status==0">启用</div>-->
          <el-tag type="danger" v-if="scope.row.status==1">禁用</el-tag>
          <el-tag type="success" v-else="scope.row.status==0">启用</el-tag>
        </template>
      </el-table-column>


    <!--  <el-table-column align="center" sortable prop="createTime" label="创建时间" min-width="120">
      </el-table-column>

      <el-table-column align="center" sortable prop="updateTime" label="更新时间" min-width="120">
      </el-table-column>-->

      <el-table-column label="操作" min-width="250">
        <template slot-scope="scope">
          <el-button size="mini"  type="success"  icon="el-icon-edit" @click="editDept(scope.$index, scope.row)" v-has="'sys:dept:edit'">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete"  @click="deleteDept(scope.$index, scope.row)" v-has="'sys:dept:delete'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>



    <!-- 添加编辑界面 -->
    <el-dialog :title="title" :visible.sync="deptFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="deptForm" :model="deptForm" :rules="deptFormRules">

        <el-form-item label="上级部门" prop="parentId" v-if="deptForm.parentId!=0">
          <tree-select v-model="deptForm.parentId" :options="deptOptions" placeholder="选择上级部门" />
        </el-form-item>

        <el-form-item label="部门名称" prop="deptName">
          <el-input size="small" v-model="deptForm.deptName"  placeholder="请输入部门名称"></el-input>
        </el-form-item>

        <el-form-item label="部门排序" prop="orderNum">
          <el-input-number size="small" v-model="deptForm.orderNum"  controls-position="right"  :min="1" ></el-input-number>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio v-model="deptForm.status" :label="0" >启用</el-radio>
          <el-radio v-model="deptForm.status" :label="1">禁用</el-radio>
        </el-form-item>
      <!--  <el-form-item label="状态" prop="status">
          <el-select size="small" v-model="postForm.status" placeholder="请选择" >
            <el-option label="启用" :value="0"></el-option>
            <el-option label="禁用" :value="1"></el-option>
          </el-select>
        </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('deptForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import TreeSelect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  export default {
    // 注册组件
    components: {
      TreeSelect
    },
    data() {
      return {
        // 请求数据参数
        formInline: {
          page: 1,
          pageSize: 10,
          deptName: '',
          status:''
        },
        loading: false, //是显示加载
        deptFormVisible: false, //控制用户添加页面显示与隐藏
        title: "添加用户",
        deptData:[], // 用户列表数据
        deptOptions:[], // 部门下拉列表数据
        selectData: [], // 选择数据
        deptForm:{
          deptId:'',
          parentId:'',
          deptName:'',
          orderNum:'',
          status:""
        },
        // rules表单验证
        deptFormRules: {
          deptName: [
            { required: true, message: '请输入岗位名称', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '请输入岗位排序', trigger: 'blur' },
            {
              pattern: /^[1-9]*[0-9]*$/,
              required: true,
              message: '请输入正确正整数',
              trigger: 'blur'
            }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ]
        },
      }
    },
    created(){
      this.getDeptList()
    },
    methods: {
      search(){
        this.getDeptList()
      },
      clearForm(){
        if (this.$refs['formInline']) {
          this.$refs['formInline'].resetFields()
        }
        this.getDeptList()
      },
      getDeptList(){
        this.loading = true;
        this.$axios.getDeptList(this.formInline).then(res=>{
          this.loading=false
          if (res.code === 0) {
            this.deptData=res.data;
          }

        }).catch(err=>{
          this.loading=false
          console.log("getDeptListErr",err);
        })
      },
      // 选择复选框事件
      selectChange(val) {
        this.selectData = val
        console.log('selectData',this.selectData)
      },
      editDept(index,row){
        this.deptFormVisible = true
        this.title="编辑岗位"
        if (this.$refs['deptForm']) {
          this.$refs['deptForm'].resetFields()
        }
        this.getDeptTreeList();
        this.deptForm.deptId=row.id
        this.deptForm.parentId=row.parentId
        this.deptForm.deptName=row.label
        this.deptForm.orderNum=row.orderNum
        // 此处有坑如果服务器返回的是字符串的需要转Number不然不会回显
        this.deptForm.status=Number(row.status)
      },
      deleteDept(index,row){
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.deleteDept({id:row.id}).then(res=>{
            if (res.code===0) {
              this.getDeptList()
              this.$message({
                type: 'success',
                message: '删除成功！'
              })
            }
          }).catch(err=>{
              console.log('deleteDeptErr',err)
          })
        })
      },
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == 'edit') {
          this.deptFormVisible = false
        }
      },
      addDept(){
        this.deptFormVisible = true
        this.title='添加岗位'
        if( this.$refs['deptForm']){
          this.$refs['deptForm'].resetFields()
        }
        this.deptForm.deptId=""
        // 把v-modle绑定的值设为null，必须是null，不能是‘null’，初始化的时候才不会出现unknown。
        this.deptForm.parentId=null
        this.deptForm.deptName=""
        this.deptForm.orderNum=""
        this.deptForm.status=""
        this.getDeptTreeList();
      },
      getDeptTreeList(){
        this.$axios.getDeptTreeList(this.formInline).then(res=>{
          if (res.code === 0) {
            this.deptOptions=res.data;
            console.log("deptOptions"+this.deptOptions);
          }

        }).catch(err=>{
          console.log("deptOptions",err);
        })
      },
      submitForm(deptForm){
        this.$refs[deptForm].validate(valid => {

          if (valid) {
            this.loading = true
            // 请求方法
            if(this.deptForm.deptId){
               this.$axios.updateDept(this.deptForm).then(res=>{
                 this.loading = false
                 if (res.code===0) {
                   this.deptFormVisible = false
                   this.getDeptList()
                   this.$message({
                     type: 'success',
                     message: '修改成功！'
                   })
                 }
               }).catch(err=>{
                 console.log('updateDeptErr',err);
                 this.loading = false
               })
            }else{
              this.$axios.saveDept(this.deptForm).then(res => {
                  this.loading = false
                  if (res.code===0) {
                    this.deptFormVisible = false
                    this.getDeptList()
                    this.$message({
                      type: 'success',
                      message: '保存成功！'
                    })
                  }
                }).catch(err=>{
                  console.log('saveDeptErr',err);
                  this.loading = false
              })


            }
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }
</style>


